package com.zby.admin.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.admin.mapper.ReturnRecordsMapper;
import com.zby.admin.pojo.ReturnReason;
import com.zby.admin.pojo.ReturnRecords;
import com.zby.admin.pojo.dto.ReturnRecordsDto;
import com.zby.admin.service.ReturnReasonService;
import com.zby.admin.service.ReturnRecordsService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReturnRecordsServiceImp extends ServiceImpl<ReturnRecordsMapper, ReturnRecords> implements ReturnRecordsService {
    @Resource
    private ReturnRecordsMapper returnRecordsMapper;
    @Resource
    private ReturnReasonService returnReasonService;
    @Override
    public boolean insertReturnRecords(ReturnRecordsDto returnRecordsDto, HttpServletRequest request) {
        ReturnRecords returnRecords = new ReturnRecords();
        returnRecords.setProductOrderCode(returnRecordsDto.getProductOrderCode());
        returnRecords.setProductTitle(returnRecordsDto.getProductTitle());
        returnRecords.setReason(returnRecordsDto.getReason());
        returnRecords.setDescription(returnRecordsDto.getDescription());
        int insert = returnRecordsMapper.insert(returnRecords);
        return insert > 0;
    }

    @Override
    public ReturnRecordsDto getReturnRecordsByCode(Long code) {
        QueryWrapper<ReturnRecords> recordsQueryWrapper = new QueryWrapper<>();
        recordsQueryWrapper.eq("product_order_code", code);
        ReturnRecords records = returnRecordsMapper.selectOne(recordsQueryWrapper);
        if (records != null) {
            ReturnRecordsDto returnRecordsDto = new ReturnRecordsDto();
            returnRecordsDto.setId(records.getId());
            returnRecordsDto.setProductOrderCode(records.getProductOrderCode());
            returnRecordsDto.setProductTitle(records.getProductTitle());
            returnRecordsDto.setReason(records.getReason());
            returnRecordsDto.setDescription(records.getDescription());
            return returnRecordsDto;
        }
        return null;
    }
    
    @Override
    public Page<ReturnRecordsDto> getAllReturnRecords() {
        // 默认查询第1页，每页10条记录
        return getAllReturnRecords(1, 10);
    }
    
    @Override
    public Page<ReturnRecordsDto> getAllReturnRecords(Integer pageNum, Integer pageSize) {
        // 创建分页对象
        Page<ReturnRecords> page = new Page<>(pageNum, pageSize);
        
        // 查询所有退货记录
        Page<ReturnRecords> returnRecordsPage = returnRecordsMapper.selectPage(page, null);
        
        // 转换为DTO对象
        Page<ReturnRecordsDto> dtoPage = new Page<>(returnRecordsPage.getCurrent(), returnRecordsPage.getSize(), returnRecordsPage.getTotal());
        
        // 转换数据
        List<ReturnRecordsDto> dtoList = returnRecordsPage.getRecords().stream().map(record -> {
            ReturnRecordsDto dto = new ReturnRecordsDto();
            dto.setId(record.getId());
            dto.setProductOrderCode(record.getProductOrderCode());
            dto.setProductTitle(record.getProductTitle());
            // 根据id查询原因
            ReturnReason byId = returnReasonService.getById(record.getReason());
            dto.setReason(byId.getReason());
            dto.setDescription(record.getDescription());
            return dto;
        }).collect(Collectors.toList());
        
        dtoPage.setRecords(dtoList);
        return dtoPage;
    }
    
    @Override
    public List<ReturnRecordsDto> getAllReturnRecordsList() {
        // 查询所有退货记录
        List<ReturnRecords> returnRecordsList = returnRecordsMapper.selectList(null);
        
        // 转换为DTO对象列表
        return returnRecordsList.stream().map(record -> {
            ReturnRecordsDto dto = new ReturnRecordsDto();
            dto.setId(record.getId());
            dto.setProductOrderCode(record.getProductOrderCode());
            dto.setProductTitle(record.getProductTitle());
            dto.setReason(record.getReason());
            dto.setDescription(record.getDescription());
            return dto;
        }).collect(Collectors.toList());
    }
}